﻿(function (root, factroy) {
    typeof root.layui === 'object' && layui.define ? layui.define(['jquery'], function (mods) { mods('lazyload', factroy(layui.jquery, window, document)) }) :
    factroy(root.$, window, document);
}(this, function (e, t, o, i) {
    var n = e(t);
    e.fn.lazyload = function (r) {
        function f() {
            var t = 0;
            a.each(function () {
                var o = e(this);
                if (!h.skip_invisible || o.is(":visible")) if (e.abovethetop(this, h) || e.leftofbegin(this, h));
                else if (e.belowthefold(this, h) || e.rightoffold(this, h)) {
                    if (++t > h.failure_limit) return !1
                } else o.trigger("appear"),
                t = 0
            })
        }
        var l, a = this,
        h = {
            threshold: 0,
            failure_limit: 0,
            event: "scroll",
            effect: "show",
            container: t,
            data_attribute: "original",
            skip_invisible: !0,
            appear: null,
            load: null,
            placeholder: "/images/nopic.jpg"
        };
        return r && (i !== r.failurelimit && (r.failure_limit = r.failurelimit, delete r.failurelimit), i !== r.effectspeed && (r.effect_speed = r.effectspeed, delete r.effectspeed), e.extend(h, r)),
        l = h.container === i || h.container === t ? n : e(h.container),
        0 === h.event.indexOf("scroll") && l.bind(h.event,
        function () {
            return f()
        }),
        this.each(function () {
            var t = this,
            o = e(t);
            t.loaded = !1,
            (o.attr("src") === i || o.attr("src") === !1) && o.attr("src", h.placeholder),
            o.one("appear",
            function () {
                if (!this.loaded) {
                    if (h.appear) {
                        var i = a.length;
                        h.appear.call(t, i, h)
                    }
                    e("<img />").bind("load",
                    function () {
                        var i = o.data(h.data_attribute);
                        o.hide(),
                        o.is("img") ? o.attr("src", i) : o.css("background-image", "url('" + i + "')"),
                        o[h.effect](h.effect_speed),
                        t.loaded = !0;
                        var n = e.grep(a,
                        function (e) {
                            return !e.loaded
                        });
                        if (a = e(n), h.load) {
                            var r = a.length;
                            h.load.call(t, r, h)
                        }
                    }).attr("src", o.data(h.data_attribute))
                }
            }),
            0 !== h.event.indexOf("scroll") && o.bind(h.event,
            function () {
                t.loaded || o.trigger("appear")
            })
        }),
        n.bind("resize",
        function () {
            f()
        }),
        /iphone|ipod|ipad.*os 5/gi.test(navigator.appVersion) && n.bind("pageshow",
        function (t) {
            t.originalEvent && t.originalEvent.persisted && a.each(function () {
                e(this).trigger("appear")
            })
        }),
        e(o).ready(function () {
            f()
        }),
        this
    },
    e.belowthefold = function (o, r) {
        var f;
        return f = r.container === i || r.container === t ? (t.innerHeight ? t.innerHeight : n.height()) + n.scrollTop() : e(r.container).offset().top + e(r.container).height(),
        f <= e(o).offset().top - r.threshold
    },
    e.rightoffold = function (o, r) {
        var f;
        return f = r.container === i || r.container === t ? n.width() + n.scrollLeft() : e(r.container).offset().left + e(r.container).width(),
        f <= e(o).offset().left - r.threshold
    },
    e.abovethetop = function (o, r) {
        var f;
        return f = r.container === i || r.container === t ? n.scrollTop() : e(r.container).offset().top,
        f >= e(o).offset().top + r.threshold + e(o).height()
    },
    e.leftofbegin = function (o, r) {
        var f;
        return f = r.container === i || r.container === t ? n.scrollLeft() : e(r.container).offset().left,
        f >= e(o).offset().left + r.threshold + e(o).width()
    },
    e.inviewport = function (t, o) {
        return !(e.rightoffold(t, o) || e.leftofbegin(t, o) || e.belowthefold(t, o) || e.abovethetop(t, o))
    },
    e.extend(e.expr[":"], {
        "below-the-fold": function (t) {
            return e.belowthefold(t, {
                threshold: 0
            })
        },
        "above-the-top": function (t) {
            return !e.belowthefold(t, {
                threshold: 0
            })
        },
        "right-of-screen": function (t) {
            return e.rightoffold(t, {
                threshold: 0
            })
        },
        "left-of-screen": function (t) {
            return !e.rightoffold(t, {
                threshold: 0
            })
        },
        "in-viewport": function (t) {
            return e.inviewport(t, {
                threshold: 0
            })
        },
        "above-the-fold": function (t) {
            return !e.belowthefold(t, {
                threshold: 0
            })
        },
        "right-of-fold": function (t) {
            return e.rightoffold(t, {
                threshold: 0
            })
        },
        "left-of-fold": function (t) {
            return !e.rightoffold(t, {
                threshold: 0
            })
        }
    })
    return { mod: 'lazyload', v: '1.9.0' };
}));